package com.youlu.campus.admin.wechat;
import com.youlu.campus.admin.utils.AuthUtils;
import com.youlu.campus.common.base.RestResult;
import com.youlu.campus.common.base.RestResultBuilder;
import com.youlu.campus.entity.VO.req.WechatAuthBlackListQueryVO;
import com.youlu.campus.entity.WechatAuthBlackList;
import com.youlu.campus.service.wechat.WechatAuthBlackListService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.repository.support.PageableExecutionUtils;
import org.springframework.web.bind.annotation.*;
import javax.validation.Valid;
import java.util.ArrayList;

@RestController
public class WechatAuthBlackListController {
    @Autowired
    private WechatAuthBlackListService wechatAuthBlackListService;

    @PostMapping("/wechat/auth-black-list/list")
    public RestResult list(@RequestBody @Valid WechatAuthBlackListQueryVO req) {
        String comId = AuthUtils.getComId();
        Page<WechatAuthBlackList> datas = wechatAuthBlackListService.list(req);
        if (datas == null) {
            PageRequest pageRequest = PageRequest.of(req.getPage() - 1, req.getPageSize());
            datas = PageableExecutionUtils.getPage(new ArrayList<>(), pageRequest, () -> 0L);
        }
        return RestResultBuilder.builder().success().data(datas).build();
    }

    @PostMapping("/wechat/auth-black-list/create")
    public RestResult create(@RequestBody @Valid WechatAuthBlackList req) {
        String comId = AuthUtils.getComId();
        boolean re = wechatAuthBlackListService.create(req);
        if (re) {
            return RestResultBuilder.builder().success().data(re).build();
        }
        return RestResultBuilder.builder().failure().build();
    }

    @PostMapping("/wechat/auth-black-list/update")
    public RestResult update(@RequestBody @Valid WechatAuthBlackList req) {
        String comId = AuthUtils.getComId();
        boolean re = wechatAuthBlackListService.update(req);
        if (re) {
            return RestResultBuilder.builder().success().data(re).build();
        }
        return RestResultBuilder.builder().failure().build();
    }

    @GetMapping("/wechat/auth-black-list/find-one")
    public RestResult findOne(@RequestParam("id") String id) {
        String comId = AuthUtils.getComId();
        WechatAuthBlackList re = wechatAuthBlackListService.findOne(id);
        return RestResultBuilder.builder().success().data(re).build();
    }

    @GetMapping("/wechat/auth-black-list/delete")
    public RestResult delete(@RequestParam("id") String id) {
        String comId = AuthUtils.getComId();
        boolean re = wechatAuthBlackListService.delete(id);
        if (re) {
            return RestResultBuilder.builder().success().data(re).build();
        }
        return RestResultBuilder.builder().failure().build();
    }
}
